package cn.springcloud.book;

import cn.springcloud.book.service.ServiceA;
import com.codingapi.txlcn.tc.config.EnableDistributedTransaction;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
//@EnableDistributedTransaction
@MapperScan(value = "cn.springcloud.book.dao")
@Slf4j
@EnableSwagger2
public class ClientCApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientCApplication.class, args);
    }

    @Autowired
    private ServiceA serviceA;

    @RequestMapping("/test")
    public String test(HttpServletRequest request) throws Exception {
        System.out.println("----------------header----------------");
        Enumeration headerNames = request.getHeaderNames();
//        while (headerNames.hasMoreElements()) {
//            String key = (String) headerNames.nextElement();
//            System.out.println(key + ": " + request.getHeader(key));
//        }
        serviceA.execute(String.valueOf(System.currentTimeMillis()));
        System.out.println("----------------header----------------");
        return "hellooooooooooooooo!";
    }

    @Bean
    public Docket swaggerPersonApi10() {
        String DEFAULT_TOKEN = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIzMmJkNjFkZTRlN2Q0NDQ1YjY0MWEyODg0ZDcwZTVlZiIsImlhdCI6MTU2NjIwMzY0MCwic3ViIjoic2hvcF91c2VyIiwiaXNzIjoid3d3Lm9rY2xvdWQuY29tIiwiZXhwIjoxNTY2ODA4NDQwfQ.AQOQyKp0EsEtak4okSmjx-22V0AYheoY-qrzEGXy4Ns";

        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("Authorization").description("令牌-token").modelRef(new ModelRef("string")).parameterType("header")
                .defaultValue(DEFAULT_TOKEN)
                .required(false)
                .build();
        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.springcloud.book.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(
                        new ApiInfoBuilder()
                                .version("1.0")
                                .title("Original Service API")
                                .description("Original Service API v1.0")
                                .build()
                ).globalOperationParameters(pars);
    }

}
